package cn.jhc.lifecycle;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import static cn.jhc.lifecycle.LogUtil.output;

/**
 * Created by luyanfei on 16-2-29.
 */
public class FirstFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        output();
        View rootView = inflater.inflate(R.layout.fragment_first, container, false);

        Button showDialogButton = (Button) rootView.findViewById(R.id.btn_show_dialog);
        showDialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new LifeDialog().show(getFragmentManager(), null);
            }
        });

        Button alertButton = (Button) rootView.findViewById(R.id.btn_show_alert_dialog);
        alertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("AlertDialog").setMessage("这是AlertDialog")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        })
                        .create()
                        .show();
            }
        });

        Button activityButton = (Button) rootView.findViewById(R.id.btn_switch_activity);
        activityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), SecondActivity.class);
                startActivity(intent);
            }
        });
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        output();
    }

    @Override
    public void onStart() {
        super.onStart();
        output();
    }

    @Override
    public void onResume() {
        super.onResume();
        output();
    }

    @Override
    public void onPause() {
        super.onPause();
        output();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        output();
    }

    @Override
    public void onStop() {
        super.onStop();
        output();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        output();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        output();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        output();
    }
}
